//
//  KeyInvalid.m
//  UCLBRTStatic
//
//  Created by myApple on 16/2/23.
//  Copyright (c) 2016年 myApple. All rights reserved.
//

#import "KeyInvalid.h"
#import "HttpHelper.h"
#import "Constants.h"
#import "MyRsa.h"
#import "NSString+check.h"
#import "DateHelper.h"
#import "CFNetworkErrors.h"

@interface KeyInvalid()
{
    NSString * accountSid;
    NSString * token;
}

@end

@implementation KeyInvalid

-(id)initWithAccounSid:(NSString *)accountSid_ token:(NSString *)token_
{
    self = [super init];
    if (self) {
        accountSid = accountSid_;
        token = token_;
    }
    return self;
}

#pragma mark ---------- 对象方法通过房间号取消房卡
-(void)cancelByRoomWithCommunityNo:(NSString *)communityNo mobile:(NSString *)mobile buildNo:(NSString *)buildNo floorNo:(NSString *)floorNo roomNo:(NSString *)roomNo startTime:(NSString *)startTime endTime:(NSString *)endTime success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    __block NSMutableDictionary * statusDict = [[NSMutableDictionary alloc]init];
    NSMutableDictionary * paramDict = [[NSMutableDictionary alloc]init];
    __block NSError * error;
    if ([accountSid isEmpty] || accountSid == nil) {
        [statusDict setValue:@"账号错误" forKey:ErrorInfo];
        error = [[NSError alloc]initWithDomain:RequestError code:UCLBRT_TokenAuthorizationFailed userInfo:statusDict];
        if (failure) {
            failure(error);
        }
        return;
    }
    if ([token isEmpty] || token == nil) {
        [statusDict setValue:@"无此token账号" forKey:ErrorInfo];
        error = [[NSError alloc]initWithDomain:RequestError code:UCLBRT_NoSuchAccount userInfo:statusDict];
        if (failure) {
            failure(error);
        }
        return;
    }
    if ([mobile isEmpty] || mobile == nil) {
        [statusDict setValue:@"手机号格式不正确" forKey:ErrorInfo];
        error = [[NSError alloc]initWithDomain:RequestError code:UCLBRT_MobileFormatNotCorrect userInfo:statusDict];
        if (failure) {
            failure(error);
        }
        return;
    }
    if ([communityNo isEmpty] || communityNo == nil) {
        [statusDict setValue:@"集群编号不正确" forKey:ErrorInfo];
        error = [[NSError alloc]initWithDomain:RequestError code:UCLBRT_CommunityNoFormatNotCorrect userInfo:statusDict];
        if (failure) {
            failure(error);
        }
        return;
    }
    if ([roomNo isEmpty] || roomNo == nil) {
        [statusDict setValue:@"房间编号不正确" forKey:ErrorInfo];
        error = [[NSError alloc]initWithDomain:RequestError code:UCLBRT_RoomNoNotExist userInfo:statusDict];
        if (failure) {
            failure(error);
        }
        return;
    }
    if (startTime.length != 10) {
        [statusDict setValue:@"开始时间格式不正确" forKey:ErrorInfo];
        error = [[NSError alloc]initWithDomain:RequestError code:UCLBRT_StartTimeFormatNotCorrect userInfo:statusDict];
        if (failure) {
            failure(error);
        }
        return;
    }
    if (endTime.length != 10) {
        [statusDict setValue:@"结束时间格式不正确" forKey:ErrorInfo];
        error = [[NSError alloc]initWithDomain:RequestError code:UCLBRT_EndTimeFormatNotCorrect userInfo:statusDict];
        if (failure) {
            failure(error);
        }
        return;
    }
    if (buildNo == nil) {
        buildNo = @"";
    }
    if (floorNo == nil) {
        floorNo = @"";
    }


    NSMutableString * paramString = [[NSMutableString alloc]init];
    [paramString appendString:[NSString stringWithFormat:@"sid=%@&token=%@&mobile=%@&communityNo=%@&roomNo=%@&buildNo=%@&floorNo=%@&startTime=%@&endTime=%@&timestamp=%@",accountSid,token,mobile,communityNo,roomNo,buildNo,floorNo,startTime,endTime,[DateHelper getTimestamp]]];
    
    //参数加密
    MyRsa * rsa = [[MyRsa alloc]init];
    NSString * encrypByRSAString = [rsa RSAEncrypotoTheData:paramString key:PublicKey];
    
    [paramDict setValue:Version forKey:@"version"];
    [paramDict setValue:encrypByRSAString forKey:@"data"];
     HttpHelper * httpReqest = [[HttpHelper alloc]init];
    [httpReqest postRequest:POST(InvalidByParamUrl) RequestParams:paramDict];
    httpReqest.resultBlock = ^(NSData * downloadData,NSError * connectionError)
    {
        if (downloadData) {
            id obj = [NSJSONSerialization JSONObjectWithData:downloadData options:NSJSONReadingMutableContainers error:nil];
            if ([[obj objectForKey:@"status"]integerValue] == 200) {
                if (success) {
                    success(obj);
                }
            }
            else
            {
                [statusDict setValue:[obj objectForKey:@"info"] forKey:ErrorInfo];
                error = [[NSError alloc]initWithDomain:RequestError code:[[obj objectForKey:@"status"] integerValue] userInfo:statusDict];
                if (failure) {
                    failure(error);
                }
            }

        }
        else
        {
            if (success) {
                success(connectionError);
            }
        }
        
    };

}

#pragma mark -----------类方法通过房间号取消房卡
+(void)cancelByRoomWithAccountSid:(NSString *)accountSid token:(NSString *)token communityNo:(NSString *)communityNo mobile:(NSString *)mobile buildNo:(NSString *)buildNo floorNo:(NSString *)floorNo roomNo:(NSString *)roomNo startTime:(NSString *)startTime endTime:(NSString *)endTime success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    __block NSMutableDictionary * statusDict = [[NSMutableDictionary alloc]init];
    NSMutableDictionary * paramDict = [[NSMutableDictionary alloc]init];
    __block NSError * error;
    if ([accountSid isEmpty] || accountSid == nil) {
        [statusDict setValue:@"账号错误" forKey:ErrorInfo];
        error = [[NSError alloc]initWithDomain:RequestError code:UCLBRT_TokenAuthorizationFailed userInfo:statusDict];
        if (failure) {
            failure(error);
        }
        return;
    }
    if ([token isEmpty] || token == nil) {
        [statusDict setValue:@"无此token账号" forKey:ErrorInfo];
        error = [[NSError alloc]initWithDomain:RequestError code:UCLBRT_NoSuchAccount userInfo:statusDict];
        if (failure) {
            failure(error);
        }
        return;
    }
    if ([mobile isEmpty] || mobile == nil) {
        [statusDict setValue:@"手机号格式不正确" forKey:ErrorInfo];
        error = [[NSError alloc]initWithDomain:RequestError code:UCLBRT_MobileFormatNotCorrect userInfo:statusDict];
        if (failure) {
            failure(error);
        }
        return;
    }
    if ([communityNo isEmpty] || communityNo == nil) {
        [statusDict setValue:@"集群编号不正确" forKey:ErrorInfo];
        error = [[NSError alloc]initWithDomain:RequestError code:UCLBRT_CommunityNoFormatNotCorrect userInfo:statusDict];
        if (failure) {
            failure(error);
        }
        return;
    }
    if ([roomNo isEmpty] || roomNo == nil) {
        [statusDict setValue:@"房间编号不正确" forKey:ErrorInfo];
        error = [[NSError alloc]initWithDomain:RequestError code:UCLBRT_RoomNoNotExist userInfo:statusDict];
        if (failure) {
            failure(error);
        }
        return;
    }
    if (startTime.length != 10) {
        [statusDict setValue:@"开始时间格式不正确" forKey:ErrorInfo];
        error = [[NSError alloc]initWithDomain:RequestError code:UCLBRT_StartTimeFormatNotCorrect userInfo:statusDict];
        if (failure) {
            failure(error);
        }
        return;
    }
    if (endTime.length != 10) {
        [statusDict setValue:@"结束时间格式不正确" forKey:ErrorInfo];
        error = [[NSError alloc]initWithDomain:RequestError code:UCLBRT_EndTimeFormatNotCorrect userInfo:statusDict];
        if (failure) {
            failure(error);
        }
        return;
    }
    if (buildNo == nil) {
        buildNo = @"";
    }
    if (floorNo == nil) {
        floorNo = @"";
    }
    
    NSMutableString * paramString = [[NSMutableString alloc]init];
    [paramString appendString:[NSString stringWithFormat:@"sid=%@&token=%@&mobile=%@&communityNo=%@&roomNo=%@&buildNo=%@&floorNo=%@&startTime=%@&endTime=%@&timestamp=%@",accountSid,token,mobile,communityNo,roomNo,buildNo,floorNo,startTime,endTime,[DateHelper getTimestamp]]];
    
    //参数加密
    MyRsa * rsa = [[MyRsa alloc]init];
    NSString * encrypByRSAString = [rsa RSAEncrypotoTheData:paramString key:PublicKey];
    
    [paramDict setValue:Version forKey:@"version"];
    [paramDict setValue:encrypByRSAString forKey:@"data"];
    HttpHelper * httpReqest = [[HttpHelper alloc]init];
    [httpReqest postRequest:POST(InvalidByParamUrl) RequestParams:paramDict];
    httpReqest.resultBlock = ^(NSData * downloadData,NSError * connectionError)
    {
        if (downloadData) {
            id obj = [NSJSONSerialization JSONObjectWithData:downloadData options:NSJSONReadingMutableContainers error:nil];
            if ([[obj objectForKey:@"status"]integerValue] == 200) {
                if (success) {
                    success(obj);
                }
            }
            else
            {
                [statusDict setValue:[obj objectForKey:@"info"] forKey:ErrorInfo];
                error = [[NSError alloc]initWithDomain:RequestError code:[[obj objectForKey:@"status"] integerValue] userInfo:statusDict];
                if (failure) {
                    failure(error);
                }
            }
        }
        else
        {
            if (failure) {
                failure(connectionError);
            }
        }
        
    };

}

#pragma mark ---------- 对象方法通过唯一标示取消房卡
-(void)cancelByIdWithIdentityNo:(NSString *)identityNo success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    __block NSMutableDictionary * statusDict = [[NSMutableDictionary alloc]init];
    NSMutableDictionary * paramDict = [[NSMutableDictionary alloc]init];
    __block NSError * error;
    if ([accountSid isEmpty] || accountSid == nil) {
        [statusDict setValue:@"账号错误" forKey:ErrorInfo];
        error = [[NSError alloc]initWithDomain:RequestError code:UCLBRT_TokenAuthorizationFailed userInfo:statusDict];
        if (failure) {
            failure(error);
        }
        return;
    }
    if ([token isEmpty] || token == nil) {
        [statusDict setValue:@"无此token账号" forKey:ErrorInfo];
        error = [[NSError alloc]initWithDomain:RequestError code:UCLBRT_NoSuchAccount userInfo:statusDict];
        if (failure) {
            failure(error);
        }
        return;
    }
    
    if ([identityNo isEmpty] || identityNo == nil) {
        [statusDict setValue:@"唯一标示号不正确" forKey:@"errorInfo"];
        error = [[NSError alloc]initWithDomain:@"1023" code:5104 userInfo:statusDict];
        if (failure) {
            failure(error);
        }
        return;
    }
    
    NSMutableString * paramString = [[NSMutableString alloc]init];
    [paramString appendString:[NSString stringWithFormat:@"sid=%@&token=%@&no=%@&timestamp=%@",accountSid,token,identityNo,[DateHelper getTimestamp]]];
    
    //参数加密
    MyRsa * rsa = [[MyRsa alloc]init];
    NSString * encrypByRSAString = [rsa RSAEncrypotoTheData:paramString key:PublicKey];
    
    [paramDict setValue:Version forKey:@"version"];
    [paramDict setValue:encrypByRSAString forKey:@"data"];
    HttpHelper * httpReqest = [[HttpHelper alloc]init];
    [httpReqest postRequest:POST(InvalidByIdUrl) RequestParams:paramDict];
    httpReqest.resultBlock = ^(NSData * downloadData,NSError * connectionError)
    {
        if (downloadData) {
            id obj = [NSJSONSerialization JSONObjectWithData:downloadData options:NSJSONReadingMutableContainers error:nil];
            if ([[obj objectForKey:@"status"]integerValue] == 200) {
                if (success) {
                    success(obj);
                }
            }
            else
            {
                [statusDict setValue:[obj objectForKey:@"info"] forKey:ErrorInfo];
                error = [[NSError alloc]initWithDomain:RequestError code:[[obj objectForKey:@"status"] integerValue] userInfo:statusDict];
                if (failure) {
                    failure(error);
                }
            }

        }
        else
        {
            if (failure) {
                failure(connectionError);
            }
        }
        
    };

}

#pragma mark -----------类方法通过唯一标示取消房卡
+(void)cancelByIdWithAccountSid:(NSString *)accountSid token:(NSString *)token identityNo:(NSString *)identityNo success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    __block NSMutableDictionary * statusDict = [[NSMutableDictionary alloc]init];
    NSMutableDictionary * paramDict = [[NSMutableDictionary alloc]init];
    __block NSError * error;
    if ([accountSid isEmpty] || accountSid == nil) {
        [statusDict setValue:@"账号错误" forKey:ErrorInfo];
        error = [[NSError alloc]initWithDomain:RequestError code:UCLBRT_TokenAuthorizationFailed userInfo:statusDict];
        if (failure) {
            failure(error);
        }
        return;
    }
    if ([token isEmpty] || token == nil) {
        [statusDict setValue:@"无此token账号" forKey:ErrorInfo];
        error = [[NSError alloc]initWithDomain:RequestError code:UCLBRT_NoSuchAccount userInfo:statusDict];
        if (failure) {
            failure(error);
        }
        return;
    }
    
    if ([identityNo isEmpty] || identityNo == nil) {
        [statusDict setValue:@"唯一标示号不正确" forKey:@"errorInfo"];
        error = [[NSError alloc]initWithDomain:@"1023" code:5104 userInfo:statusDict];
        if (failure) {
            failure(error);
        }
        return;
    }
    NSMutableString * paramString = [[NSMutableString alloc]init];
    [paramString appendString:[NSString stringWithFormat:@"sid=%@&token=%@&no=%@&timestamp=%@",accountSid,token,identityNo,[DateHelper getTimestamp]]];
    
    //参数加密
    MyRsa * rsa = [[MyRsa alloc]init];
    NSString * encrypByRSAString = [rsa RSAEncrypotoTheData:paramString key:PublicKey];
    
    [paramDict setValue:Version forKey:@"version"];
    [paramDict setValue:encrypByRSAString forKey:@"data"];
    HttpHelper * httpReqest = [[HttpHelper alloc]init];
    [httpReqest postRequest:POST(InvalidByIdUrl) RequestParams:paramDict];
    httpReqest.resultBlock = ^(NSData * downloadData,NSError * connectionError)
    {
        if (downloadData) {
            id obj = [NSJSONSerialization JSONObjectWithData:downloadData options:NSJSONReadingMutableContainers error:nil];
            if ([[obj objectForKey:@"status"]integerValue] == 200) {
                if (success) {
                    success(obj);
                }
            }
            else
            {
                [statusDict setValue:[obj objectForKey:@"info"] forKey:ErrorInfo];
                error = [[NSError alloc]initWithDomain:RequestError code:[[obj objectForKey:@"status"] integerValue] userInfo:statusDict];
                if (failure) {
                    failure(error);
                }
            }
        }
        else
        {
            if (failure) {
                failure(connectionError);
            }
        }
        
    };
    

}

@end
